<?php
declare (strict_types=1);

namespace app\common\model\system;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class AdminRules extends Model
{
    use SoftDelete;

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * 获取无限极分类
     * @access public static
     * @param string $tips 名称格式
     * @param int $pid 栏目父ID
     * @param array $array 引用数组
     * @param int $blank 替换字符
     * @param int $level 栏目等级
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getListRule(string $tips = '', int $pid = 0, array &$array = [], int $blank = 0, int $level = 0): array
    {
        $result = self::where('pid', $pid)->select()->toArray();
        foreach ($result as $key => $value) {
            if (!empty($tips)) {
                $catename = $tips . $value['title'];
                $value['title'] = str_repeat('', $blank) . $catename;
            }
            $value['_level'] = $level;
            $array[] = $value;
            unset($result[$key]);
            self::getListRule($tips, $value['id'], $array, $blank + 1, $level + 1);
        }

        return $array;
    }

    /**
     * 返回栏目树形结构
     *
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getListTree()
    {
        $array = self::field('*,title as name')->order('sort asc')->select()->toArray();
        foreach ($array as $key => $value) {
            $array[$key]['name'] = __($value['name']);
            $array[$key]['title'] = __($value['title']);
        }
        if (is_array($array) && !empty($array)) {
            return list_to_tree($array);
        }
    }

    /**
     * 字段修改器
     *
     * @param [type] $value
     * @return void
     */
    public function setSortAttr($value)
    {
        if (is_empty($value)) {
            return self::max('id') + 1;
        }
        return $value;
    }

}
